package com.yf.controller.api.article;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUser;
import com.yf.consts.DaoConst;
import com.yf.entity.UserEntity;
import com.yf.response.R;
import com.yf.service.article.CampaignService;
import com.yf.service.article.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/campagin")
@RestController
public class ApiCampaginController {

    @Autowired
    private PlayerService playerService;

    @Autowired
    private CampaignService campaignService;


    /**
     * 通过活动ID列出所有当前活动的参赛者
     * 按投票数排名
     * 显示当前票数
     * @return
     */
    @GetMapping("/players")
    public R listPlayers(@RequestParam Map<String, Object> params){
        PageHelper.startPage(DaoConst.getPage(params),DaoConst.getLIMIT(params));
        Long campaignId = Long.valueOf((String)params.get("campaignId"));
        List playerList = playerService.showPlayerList(campaignId);
        Map map = new HashMap();
        map.put("detail",campaignService.get(campaignId));
        map.put("list",new PageInfo(playerList));
        return R.ok(map);
    }



    /**
     * 列出当前选手信息 基本信息  票数 排名  跟当前用户是否已投票
     * @param playerId
     * @return
     */
    @GetMapping("/player/{playerId}")
    public R showPlayer(@PathVariable Long playerId,@LoginUser UserEntity userEntity){
        return R.ok(playerService.showPlayer(playerId,userEntity.getId()));
    }

    /**
     * 投票  根据活动规则判断是否可以投票
     * @param playerId
     * @param userEntity
     * @return
     */
    @PostMapping("/ticket")
    public R putTicket(Long playerId, @LoginUser UserEntity userEntity){
        return R.ok(playerService.putTicket(playerId,userEntity.getId()));
    }

    /**
     * 发起参赛
     * @param userEntity
     * @param campaginId 活动ID
     * @return
     */
    @PostMapping("/player")
    public R addPlayer(@LoginUser UserEntity userEntity,Long campaginId){
        return R.ok(playerService.addPlayer(userEntity.getId(),campaginId));
    }

    /**
     * 获取正在进行的活动列表
     * @param
     * @param
     * @return
     */
    @GetMapping("/campaigns")
    public R showCampaigns(@LoginUser UserEntity userEntity){
        return R.ok(campaignService.showCampaigns());
    }

    @PostMapping("/test")
    @IgnoreAuth
    public R test(String code){
        return R.ok(code);
    }

}
